/* 
 * @Title: GetPayStatus.java 
 * @Package com.jiankangjin.transsystem.controller 
 * @Description: TODO 
 * @author neason
 * @date 2014年7月31日 下午5:25:01 
 * @version V1.0   
 */ 
package com.jiankangjin.transsystem.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.trading.dao.IOrderDataDao;
import com.jiankangjin.trading.dao.IPaymentDataDao;
import com.jiankangjin.trading.entity.OrderDataEntity;
import com.jiankangjin.trading.entity.PaymentDataEntity;
import com.jiankangjin.transsystem.stautscode.RespStatus;
import com.jiankangjin.transsystem.util.StringUtil;

/**
 * 健康金支付状态确认接口
 * v0.0.2 
 * v0.3
 */
@Controller
public class GetPayStatusController {
	private static final Log log=LogFactory.getLog(PayOrderController.class);

	@Autowired
	private IOrderDataDao iOrderDataDao;
	
	@Autowired
	private IPaymentDataDao iPaymentDataDao;
	
	/**
	 * 支付状态确认,从数据库取出支付状态返回
	 */
	@RequestMapping(value="/get_pay_status",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> getPayStatus(
			@RequestParam(required = false) String module_id,
			@RequestParam(required = false) String version,
			@RequestParam(required = false) String sign,
			@RequestParam(required = false) String sub_deal_ssn,
			@RequestParam(required = false) String order_id,
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String merchant_id,
			@RequestParam(required = false) String merchant_order_id
			) {
		StringBuilder sb = new StringBuilder();
		sb.append("module_id="+module_id);
		sb.append(" version="+version);
		sb.append(" sign="+sign);
		sb.append(" sub_deal_ssn="+sub_deal_ssn);
		sb.append(" order_id="+order_id);
		sb.append(" token="+token);
		sb.append(" merchant_id="+merchant_id);
		sb.append(" merchant_order_id="+merchant_order_id);
		log.info("支付状态确认接口:="+sb);
		Map<String, String> prams = new HashMap<String, String>();
		try {
			if ("v0.3".equals(version)) {
				prams = this.getPayStatus_V03(
	            		module_id,
	            		sub_deal_ssn,
	            		order_id,
	            		token,
	            		merchant_id,
	            		merchant_order_id
	            		);
            return prams;
			}
			if ("v0.4".equals(version)) {
				prams = getPayStatus_V04(
						module_id,
						sub_deal_ssn,
						order_id,
						token,
						merchant_id,
						merchant_order_id
						);
				//TODO 签名
				prams.put("sign", "sign");
				log.info("[交易系统订单查询]查询成功"+"sub_deal_ssn="+sub_deal_ssn+"order_id="+order_id);
				log.debug("[交易系统订单查询]返回参数为："+prams);
				return prams;
			}
			if (StringUtil.isEmpty(module_id)) {
				log.error("module_id:"+module_id+"请求模块编号  为必填项不能为空");
				prams.put("resp_status", "module_id为空");
				return prams;
			}
			if (StringUtil.isEmpty(sub_deal_ssn)) {
				log.error("sub_deal_ssn:"+sub_deal_ssn+"支付交易流水号   为必填项不能为空");
				prams.put("resp_status", "sub_deal_ssn为空");
				return prams;
			}
			
			if (StringUtil.isEmpty(order_id)) {
				log.error("order_id:"+order_id+"订单号   为必填项不能为空");
				prams.put("resp_status", "order_id为空");
				return prams;
			}
			OrderDataEntity orderData = iOrderDataDao.selectByPrimaryKey(order_id);
			String payStat = null;
			if(null != orderData) {
				PaymentDataEntity paymentData = iPaymentDataDao.selectByPrimaryKey(sub_deal_ssn);
				if(null != paymentData) {
					payStat = paymentData.getPayStat();
					prams.put("resp_status", "0000");//支付记录的支付状态
					prams.put("resp_msg", "成功");//支付记录的支付状态
					prams.put("sub_deal_stauts", payStat);
					prams.put("resp_deal_msg", "");
				} else {
					prams.put("resp_status", "9999");
					prams.put("resp_msg", "支付记录不存在");
				}
			} else {
				prams.put("resp_status", "9999");
				prams.put("resp_msg", "订单不存在");
			}
			prams.put("sign", "transsystemyessign");
			return prams;
			 
		} catch (Exception e) {
			log.error(this, e);
			prams.put("resp_status", "9999");
			prams.put("sign", "transsystemerrorsign");
			return prams;
		}
	}
	
	/**
	 * 版本=v0.3的方法
	 */
	private Map<String, String> getPayStatus_V03(String module_id,
			String sub_deal_ssn, String order_id, String token,
			String merchant_id, String merchant_order_id) {
		return null;
	}
	
	
	/**
	 * 查询订单相关信息
	 * 此方法返回参数不进行签名
	 * @param module_id
	 * @param sub_deal_ssn
	 * @param order_id
	 * @param token
	 * @param merchant_id
	 * @param merchant_order_id
	 * @return 订单相关信息
	 */
	private Map<String, String> getPayStatus_V04(String module_id,
			String sub_deal_ssn, String order_id, String token,
			String merchant_id, String merchant_order_id) {
		Map<String, String> prams = new HashMap<String, String>();
		
		//判断订单编号和支付流水号是否同时为空，如果同时为空则直接返回
		if(StringUtil.isEmpty(order_id)&&StringUtil.isEmpty(sub_deal_ssn)){
			log.error("[交易系统订单查询][参数错误]订单编号和支付流水号不能同时为空");
			prams.put("resp_status", RespStatus._0001_CODE);//响应状态
			prams.put("resp_msg", RespStatus._0001_MSG);//响应状态说明
			return prams;
		}
		//如果支付流水号不为空，则去查询支付信息
		if(StringUtil.isNotEmpty(sub_deal_ssn)){
			PaymentDataEntity paymentData = iPaymentDataDao.selectByPrimaryKey(sub_deal_ssn);
			if(null == paymentData) {
				log.error("[交易系统订单查询]"+RespStatus._0003_MSG);
				prams.put("resp_status", RespStatus._0003_CODE);//响应状态
				prams.put("resp_msg", RespStatus._0003_MSG);//响应状态说明
				return prams;
			}
			order_id = paymentData.getOrdSysId();
		}
		
		//查询订单信息
		OrderDataEntity orderData = iOrderDataDao.selectByPrimaryKey(order_id);
		if(null == orderData) {
			log.error("[交易系统订单查询]"+RespStatus._0002_MSG);
			prams.put("resp_status", RespStatus._0002_CODE);//响应状态码
			prams.put("resp_msg", RespStatus._0002_MSG);//响应状态说明
			return prams;
		}
		
		
		prams.put("resp_status", RespStatus._0000_CODE);//响应状态码
		prams.put("resp_msg", RespStatus._0000_MSG);//响应状态说明
		
		//这两个字段应该不需要
		prams.put("sub_deal_stauts", orderData.getPayStat());//支付状态
//		prams.put("sub_deal_msg", PaymentStatusCode.getDesc(orderData.getPayStat()));//支付状态说明
		
		prams.put("mer_id", orderData.getMerId());
		prams.put("user_id", orderData.getUserId());
		prams.put("out_order_id", orderData.getMerOrdId());
		prams.put("order_amount", orderData.getOrdAmt());
		prams.put("order_time", orderData.getOrdRegTime());
		prams.put("pay_amount", orderData.getPayAmt());
		prams.put("fee", orderData.getFeeAmt());
		prams.put("deal_id", orderData.getId());
		prams.put("deal_time", orderData.getOrdFinTime());
		
		prams.put("ext1", orderData.getResv1());
		prams.put("ext2", orderData.getResv2());
		prams.put("pay_status", orderData.getOrdStat());//订单状态
		prams.put("pay_status_msg", orderData.getOrdStat());//订单状态描述
		prams.put("page_url", orderData.getMerFrwdUrl());
		
		//TODO 支付相关参数暂时未定
		prams.put("bank_deal_id", orderData.getPayId());
		prams.put("pay_channel_name", "");
		prams.put("pay_type", "");
		prams.put("pay_channel_id","");
		
		return prams;
	}
}
